package com.tao.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tao.pojo.Answerer;
import com.tao.pojo.Questioner;
import com.tao.service.AnswererService;
import com.tao.service.QuestionerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: alubus
 * @Date: 2021/11/30 10:43
 * @Package: com.tao.controller
 */
@RestController
@RequestMapping("/login")
public class loginController {
    Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    AnswererService answererService;

    @Autowired
    QuestionerService questionerService;

    @PostMapping("/answerer")
//    @PsotMapping("/answerer")
//    @Cacheable(value = "AnswererLogin",key = "'username.'+#p0")
    public JSONObject answererLogin(String username,String password){
        JSONObject jobj = new JSONObject();
        JSONObject jobj2= new JSONObject();
        logger.info("loginAnswerer:"+username);
        if (username==null  || password==null ){
            jobj.put("msg","nullnullnull");
            return jobj;
        }
        Answerer answerer = answererService.selectByUsername(username);
        if (answerer==null){
            jobj.put("msg","notfound");
            return jobj;
        }
        if (answerer.getPassword().equals(password)){
            jobj.put("msg","success");
            jobj2.put("authority","answerer");
            jobj2.put("userId",answerer.getId());
            jobj2.put("head",answerer.getHead());
            jobj2.put("username",answerer.getName());
            jobj2.put("age",answerer.getAge());
            jobj2.put("classify",answerer.getClassify());
            jobj2.put("email",answerer.getEmail());
            jobj.put("answerer",jobj2);
        }else {
            jobj.put("msg","error");
        }
//        logger.info("loginAnswerer Success");

        return jobj;
    }


    @PostMapping("/questioner")
//    @PsotMapping("/answerer")
//    @Cacheable(value = "AnswererLogin",key = "'username.'+#p0")
    public JSONObject questionerLogin(String username,String password){
        JSONObject jobj = new JSONObject();
        JSONObject jobj2= new JSONObject();
        logger.info("loginQnswerer:"+username);
        if (username==null  || password==null ){
            jobj.put("msg","isNull");
            return jobj;
        }
        Questioner questioner = questionerService.selectByName(username);
        if (questioner==null){
            jobj.put("msg","notfound");
            return jobj;
        }
        if (questioner.getPassword().equals(password)){
            jobj.put("msg","success");
            jobj2.put("authority","questioner");
            jobj2.put("age",questioner.getAge());
            jobj2.put("username",questioner.getName());
            jobj2.put("email",questioner.getEmail());
            jobj2.put("userId",questioner.getId());
            jobj2.put("rank",questioner.getRank());
            jobj.put("questioner",jobj2);
        }else {
            jobj.put("msg","error");
        }
//        logger.info("loginAnswerer Success");

        return jobj;
    }

    @PostMapping("/updateQuestioner")
    public Boolean questionerLogin(Questioner questioner){
        try {
            logger.warn("Questioner:"+questioner.toString());

            if (questioner == null || questioner.getName() == null){
                return false;
            }

            QueryWrapper<Questioner> queryWrapper = new QueryWrapper<>();
            queryWrapper.select(Questioner.class, info -> !info.getColumn().equals("password"));
            queryWrapper.eq("id",questioner.getId());


            //修改信息
//            判断名字是否重复
            QueryWrapper<Questioner> q2 = new QueryWrapper<>();
            q2.eq("name",questioner.getName());
            Questioner one1 = questionerService.getOne(q2);
            if (one1 !=null ){
                if ( questioner.getId() != one1.getId()){
                    return false;
                }
            }


            Questioner one = questionerService.getOne(queryWrapper);
            one.setAge(questioner.getAge());
            one.setEmail(questioner.getEmail());
            one.setName(questioner.getName());
            boolean b = questionerService.updateById(one);

            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @PostMapping("/updateAnswerer")
    public Boolean updateAnswerer(Answerer answerer){
        try {
            logger.warn("answererUpdate:"+answerer.toString());
//            是不是空
            if (answerer== null ||answerer.getName()==null){
                return false;
            }


            QueryWrapper<Answerer> queryWrapper = new QueryWrapper<>();
            queryWrapper.select(Answerer.class, info -> !info.getColumn().equals("password"));
            queryWrapper.eq("id",answerer.getId());

            //判断名字是否重复
            QueryWrapper<Answerer> q2 = new QueryWrapper<>();
            q2.eq("name",answerer.getName());
            Answerer aname = answererService.getOne(q2);
            if (aname!=null){
                //除了自己外
                if ( answerer.getId() != aname.getId()){
                    return false;
                }
            }


            //更新
            Answerer one = answererService.getOne(queryWrapper);
            one.setAge(answerer.getAge());
            one.setEmail(answerer.getEmail());
            one.setName(answerer.getName());
            one.setHead(answerer.getHead());
            boolean b = answererService.updateById(one);



            return b;
        } catch (Exception e) {
            return false;
        }
    }
}
